package product;

import javax.swing.JInternalFrame;
import javax.swing.JPanel;

import java.awt.BorderLayout;

import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Vector;
import javax.swing.JFrame;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.Font;
import java.awt.SystemColor;
import javax.swing.SwingConstants;
import javax.swing.border.LineBorder;
import java.awt.Color;

public class DeliveryList extends JInternalFrame {
	ProductMain4 main;
	DefaultTableModel model;

	private JPanel panel;
	private JButton btnSearch;
	private JTextField textSearch;
	private JScrollPane scrollPane;
	private JTable table;
	private JLabel lblProductName;
	private JPanel panel_1;
	private JLabel lblDeliveryList;

	public DeliveryList(ProductMain4 main) {
		super("Delivery List", false, true, false, false);
		getContentPane().setBackground(SystemColor.text);
		setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		this.main = main;
		setBounds(520, 100, 637, 597);
		getContentPane().setLayout(null);
		getContentPane().add(getPanel());
		getContentPane().add(getScrollPane());
		getContentPane().add(getPanel_1());
		search();
	}

	public void search() {
		model.setRowCount(0);
		try {
//			OracleJdbc o = new OracleJdbc();
//			main.conn=o.conn;
			
			if (textSearch.getText().equals("")) {
				
				String sql = "select num, pro_nm, ea, customer_num from delivery inner join code on code = pro_code order by num";
				PreparedStatement pstmt = main.conn.prepareStatement(sql);
				ResultSet rs = pstmt.executeQuery();

				while (rs.next()) {
					Vector v = new Vector();

					v.add(rs.getString(1));
					v.add(rs.getString(2));
					v.add(rs.getString(3));
					v.add(rs.getString(4));

					model.addRow(v);
				}
				rs.close();
				pstmt.close();

			} else {
				String sql = "select num, pro_nm, ea, customer_num from delivery inner join code on code = pro_code where pro_nm like ?";
				PreparedStatement pstmt = main.conn.prepareStatement(sql);
				pstmt.setString(1, "%" + textSearch.getText() + "%");

				ResultSet rs = pstmt.executeQuery();

				while (rs.next()) {
					Vector v = new Vector();

					v.add(rs.getString(1));
					v.add(rs.getString(2));
					v.add(rs.getString(3));
					v.add(rs.getString(4));

					model.addRow(v);
				}
				rs.close();
				pstmt.close();
				
			}
			
//			main.conn.close();
		} catch (Exception e) {
		}

	}

	private JPanel getPanel() {
		if (panel == null) {
			panel = new JPanel();
			panel.setBackground(SystemColor.activeCaption);
			panel.setBounds(24, 102, 574, 50);
			panel.setLayout(null);
			panel.add(getBtnSearch());
			panel.add(getTextSearch());
			panel.add(getLblProductName());
		}
		return panel;
	}

	private JButton getBtnSearch() {
		if (btnSearch == null) {
			btnSearch = new JButton("Search");
			btnSearch.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 15));
			btnSearch.setBounds(459, 10, 100, 30);
			btnSearch.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					search();
				}
			});
		}
		return btnSearch;
	}

	private JTextField getTextSearch() {
		if (textSearch == null) {
			textSearch = new JTextField();
			textSearch.setBounds(142, 10, 304, 30);
			textSearch.addKeyListener(new KeyAdapter() {
				@Override
				public void keyPressed(KeyEvent e) {
					if (e.getKeyCode() == KeyEvent.VK_ENTER) {
						search();
					}
				}
			});
			textSearch.setColumns(10);
		}
		return textSearch;
	}

	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setBounds(24, 176, 574, 365);
			scrollPane.setViewportView(getTable());
		}
		return scrollPane;
	}

	private JTable getTable() {
		if (table == null) {
			Vector<String> vv = new Vector<String>();
			vv.add("납품번호");
			vv.add("제품이름");
			vv.add("수량");
			vv.add("사업자번호");

			model = new DefaultTableModel(vv, 0) {
				public boolean isCellEditable(int row, int column) {
					return false;
				}
			};
			table = new JTable(model);
			table.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent e) {
					if (e.getButton() == 1) {
						int q = table.getSelectedRow();

						String a = (String) table.getValueAt(q, 0);

						main.dr.textDeliveryNum.setText(a);
						main.dr.deliverySearch();
						main.dr.lblstate.setText("");
					}
				}
			});
		}
		return table;
	}
	private JLabel getLblProductName() {
		if (lblProductName == null) {
			lblProductName = new JLabel("Product Name");
			lblProductName.setBorder(new LineBorder(Color.LIGHT_GRAY));
			lblProductName.setOpaque(true);
			lblProductName.setBackground(SystemColor.info);
			lblProductName.setHorizontalAlignment(SwingConstants.CENTER);
			lblProductName.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 15));
			lblProductName.setBounds(15, 10, 118, 30);
		}
		return lblProductName;
	}
	private JPanel getPanel_1() {
		if (panel_1 == null) {
			panel_1 = new JPanel();
			panel_1.setLayout(null);
			panel_1.setBorder(new LineBorder(Color.GRAY, 2));
			panel_1.setBounds(-21, -21, 702, 104);
			panel_1.add(getLblDeliveryList());
		}
		return panel_1;
	}
	private JLabel getLblDeliveryList() {
		if (lblDeliveryList == null) {
			lblDeliveryList = new JLabel("Delivery List");
			lblDeliveryList.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 40));
			lblDeliveryList.setBounds(201, 31, 293, 61);
		}
		return lblDeliveryList;
	}
}
